package com.lagou.scala.test.Q2

import scala.util.Random

/**
 * 1. 选取对战角色
 * 2. 开始对战，用户出拳，与对手进行比较，提示胜负信息
 * 3. 猜拳结束算分，平局都加一分，获胜加二分，失败不加分
 * 4. 循环对战，当输入“n”时，终止对战，并显示对战结果
 * 5. 游戏结束后显示得分
 */
//用户类，父类，定义了一个出拳的方法
abstract class User{
  var name:String = _
  var score = 0
  var winCount = 0
  var loseCount = 0
  var drawCount = 0
  //出拳
  def showFist(fist:String): String
}
//玩家类，继承用户类
class Player extends User{

  override def showFist(fist: String): String = {
    val fists: Map[String, String] = Map("1"->"剪刀", "2"->"石头", "3"->"布")
    println(s"你出拳：${fists.get(fist).get}")
    fists.get(fist).get
  }
}
//电脑类，继承用户类
class Computer extends User{
  //随机出拳
  override def showFist(fist:String): String = {
    val fists = Array("剪刀", "石头", "布")
    var fist = fists(Random.nextInt(fists.size))
    println(s"${name}出拳！")
    println(s"${name}出拳：$fist")
    fist
  }
}
class Game{
  var gameCount = 0
  def startGame(): Unit ={
    println("----------------------欢迎进入游戏世界----------------------")
    println("*********************************************************")
    println("*************************猜拳游戏**************************")
    println("*********************************************************")
    println()
    println()
    println()
    val wolf = new Player
    wolf.name="wolf"
    val computers = Map("1"->"刘备", "2"->"张飞", "3"->"关羽")
    println("请选择要对战角色：（1.刘备 2.张飞 3.关羽）")
    val str = readLine()
    var computer = new Computer
    computer.name = computers(str)
    var startGame = false
    println("要开始么？y/n")
    var start = readLine()
    if("y" == start){
      startGame = true
    }
    while(startGame){
      play(wolf,computer)
      println("是否开始下一轮？y/n")
      var start = readLine()
      if("y" == start){
        startGame = true
      }else{
        startGame = false
      }
    }
    println("退出游戏！")
    println("----------------------------------------------------")
    showResult(wolf,computer)
  }
  //开始游戏对战
  def play(player:Player,computer:Computer): Unit ={
    println("请出拳：1、剪刀 2、石头 3、布")
    val str = readLine()
    var playFist = ""
    str match {
      case "1" => playFist=player.showFist(str)
      case "2" => playFist=player.showFist(str)
      case "3" => playFist=player.showFist(str)
      case _ => println("输入不规范，默认布");playFist=player.showFist("3")
    }
    var computerFist = computer.showFist("")
    if(playFist == "剪刀"){
      if(computerFist =="剪刀"){
        println("结果：和局！下次继续努力！")
        player.score += 1
        player.drawCount+=1
        computer.score += 1
        computer.drawCount+=1
      }else if(computerFist == "石头"){
        println("结果：你输了！加油哦！")
        computer.score += 2
        computer.winCount+=1
        player.loseCount+=1
      }else{
        println("结果：恭喜你赢了！")
        player.score += 2
        player.winCount+=1
        computer.loseCount+=1
      }
    }
    if(playFist == "石头"){
      if(computerFist =="剪刀"){
        println("结果：恭喜你赢了！")
        player.score += 2
        player.winCount+=1
        computer.loseCount+=1
      }else if(computerFist == "石头"){
        println("结果：和局！下次继续努力！")
        player.score += 1
        computer.score += 1
        player.drawCount+=1
        computer.drawCount+=1
      }else{
        println("结果：你输了！加油哦！")
        computer.score += 2
        player.loseCount+=1
        computer.winCount+=1
      }
    }
    if(playFist == "布"){
      if(computerFist =="剪刀"){
        println("结果：你输了！加油哦！")
        computer.score += 2
        player.loseCount+=1
        computer.winCount+=1
      }else if(computerFist == "石头"){
        println("结果：恭喜你赢了！")
        player.score += 2
        player.winCount+=1
        computer.loseCount+=1
      }else{
        println("结果：和局！下次继续努力！")
        player.score += 1
        computer.score += 1
        player.drawCount+=1
        computer.drawCount+=1
      }
    }
    gameCount = gameCount +1
  }
  //展示结果
  def showResult(player: Player,computer: Computer): Unit ={
    println(s"${player.name} VS ${computer.name}")
    println(s"对战次数${gameCount}次")
    println(s"姓名\t等分\t胜局\t负局\t和局")
    println(s"${player.name}\t${player.score}\t${player.winCount}\t${player.loseCount}\t${player.drawCount}")
    println(s"${computer.name}\t${computer.score}\t${computer.winCount}\t${computer.loseCount}\t${computer.drawCount}")
  }
}
object FingerGuessingGame {
  def main(args: Array[String]): Unit = {
    val game = new Game
    game.startGame()
  }
}
